import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;
import pluginUtils.CircleArrangement;
import pluginUtils.Complexe; 
import pluginUtils.DoyleCircle;
import pluginUtils.MobiusTransform;
import pluginUtils.CircleInversion;
import pluginUtils.PackCircle;
import pluginUtils.Point2D;
import pluginUtils.Pos3D;
import pluginUtils.Sphere;
import pluginUtils.SphereInversion;



public class GenericDrawer4 extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	private PrintStream output;
	private double xShift=0; 
	private double zShift=0; 
	
	

	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Green","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	private void drawCircle(PackCircle c){
		//if(intersect(c))return; 
		
		//if(c.getRadius()<0.1) return; 
		int intX=(int)(c.getX()*acoef+ccoef);
		int intY=(int)(c.getY()*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		int rank=((int)(c.getCouleur()*60))%6; 
	
			
			double Xs=c.getX(); 
			double Ys=c.getY(); 
			double rapp=Xs*Xs+Ys*Ys+1; 
			double u=Xs/rapp; 
			double v=Ys/rapp; 
			double w=(Xs*Xs+Ys*Ys)/rapp;
			
			double Xsr=c.getX(); 
			double Ysr=c.getY()+c.getRadius(); 
			double rappr=Xsr*Xsr+Ysr*Ysr+1; 
			double ur=Xsr/rappr; 
			double vr=Ysr/rappr; 
			double wr=(Xsr*Xsr+Ysr*Ysr)/rappr;
		
			double nrad=(u-ur)*(u-ur)+(v-vr)*(v-vr)+(w-wr)*(w-wr);
			//nrad=Math.sqrt(nrad)*1.414; 
			
			String s="sphere{<"+u+","+v+","+w+">,";
			
			
			s+=nrad; 
			s+=" texture{Texture"+rank+"} finish{Finish"+rank+"}}"; 
			output.println(s);
		
	}
		
	
	private void drawColoredCircle(PackCircle c){
		double xCenter=c.getX(); 
		double yCenter=c.getY(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)((xCenter-xShift)*acoef+ccoef);
		int intY=(int)((yCenter-zShift)*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
		
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
		
		/*
		primeIP.moveTo(intX,intY); 
		primeIP.drawString(c.getMyOrder()+"");
		*/
		
		String s="sphere{<"+xCenter+",0,"+yCenter+">,";
		s+=c.getRadius(); 
		s+=" scale 0.4*y texture{Texture"+generator.nextInt(7)+"} finish{Finish0}}"; 
		System.out.println(s); 
		output.println(s);
	
	}
	private void drawPoint(double x,double y){
		int radi=15; 
		int intX=(int)(x*acoef+ccoef);
		int intY=(int)(y*vcoef+wcoef);
		primeIP.fillOval(intX-radi,intY-radi,2*radi,2*radi);
	}
	
	private void drawPoint(Point2D p){
		int intX=(int)(p.getX()*acoef+ccoef);
		int intY=(int)(p.getY()*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	private void drawTriangle(double[] X,double[] Y){
		int xint[]=new int[3]; 
		int yint[]=new int[3];
		for(int i=0;i<3;i++){
			xint[i]=(int)(X[i]*acoef+ccoef); 
			yint[i]=(int)(Y[i]*vcoef+wcoef);
		}
		primeIP.fillPolygon(new Polygon(xint,yint,3)); 
		
	}
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	private void drawLine(Point2D p,Point2D q){
		int XD=(int)(p.getX()*acoef+ccoef); 
		int YD=(int)(p.getY()*vcoef+wcoef); 
		int XF=(int)(q.getX()*acoef+ccoef); 
		int YF=(int)(q.getY()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		
		
	
	
		
		double a=2/Math.sqrt(Math.sqrt(3)); 
		double h=a*Math.sqrt(3)/2; // =Math.sqrt(Math.sqrt(3))
		double u=a*Math.sqrt(3)/3;// =2/3*Math.sqrt(Math.sqrt(3))
		
		
		
		Point2D p0=new Point2D(u,0); 
		Point2D p1=new Point2D(u*Math.cos(2*Math.PI/3),u*Math.sin(2*Math.PI/3)); 
		Point2D p2=new Point2D(u*Math.cos(4*Math.PI/3),u*Math.sin(4*Math.PI/3)); 
		
		
	
		double limit=u*4; 
		double lmoins=-limit;
		
		setBounds(lmoins,limit,lmoins,limit); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(2); 
		

		setProcessor("Verifying", primeIP);
		
	
		primeIP.setColor(Color.GREEN);
		primeIP.setLineWidth(4);
		drawLine(x0,0,x1,0);
		drawLine(0,y0,0,y1);
		
	
		
		primeIP.setColor(Color.RED);
		primeIP.setLineWidth(5);
		
		drawLine(p0,p1); 
		drawLine(p1,p2); 
		drawLine(p2,p0); 
		
		
		primeIP.setColor(Color.BLACK);
		
		double coefd1=Math.sin(2*Math.PI/3)/(Math.cos(2*Math.PI/3)-1); 
		double constd1=-u*coefd1; 
		
		double coefd2=Math.sin(4*Math.PI/3)/(Math.cos(4*Math.PI/3)-1); 
		double constd2=-u*coefd2; 
		
		double step=0.1; 
		double x0=u-h; 
		/*
		for(int i=0;i<10;i++){
			drawPoint(x0, x0*coefd1+constd1); 
			drawPoint(x0, x0*coefd2+constd2); 
			x0=x0+step; 
		}
		*/
		/*
		for(int i=0;i<100;i++){
		double alpha=2*Math.PI*generator.nextDouble();
		double rad,x,y; 
		if(alpha<=2*Math.PI/3){
			x=constd1/(Math.tan(alpha)-coefd1); 
			y=Math.tan(alpha)*x; 
			
		}
		else{
			if(alpha>=4*Math.PI/3){
				x=constd2/(Math.tan(alpha)-coefd2); 
				y=Math.tan(alpha)*x; 
				
			}// if
			else{
				 x=u-h; 
				 y=Math.tan(alpha)*x; 
				
			}
		}
		rad=Math.sqrt(x*x+y*y)*generator.nextDouble(); 
		drawPoint(rad*Math.cos(alpha),rad*Math.sin(alpha)); 
		}
		*/
		
/*
		drawPoint(0.8773826753016616,0.0);
		drawPoint(-0.2555500300505565,0.6540990024088342);
		drawPoint(-0.255550030050557,-0.654099002408834);
		drawPoint(-0.4386913376508307,0.47191893510687355);
		drawPoint(-0.438691337650831,-0.3920713381101233);
		drawPoint(0.49569612140892505,0.3517676930517687);
		drawPoint(0.49569612140892505,-0.3517676930517687);
		drawPoint(-0.14187394239683113,0.0);
	*/	
		/*
		drawTriangleEqui(new double[]{0.6666666666666666,0.7333333333333333,0.6666666666666666},new double[]{0.26666666666666666,0.26666666666666666,0.3333333333333333});
		drawTriangleEqui(new double[]{0.0,0.06666666666666667,0.0},new double[]{0.5333333333333333,0.5333333333333333,0.6});
		drawTriangleEqui(new double[]{0.0,0.06666666666666667,0.0},new double[]{0.0,0.0,0.06666666666666667});
		drawTriangleEqui(new double[]{0.5333333333333333,0.6,0.5333333333333333},new double[]{0.0,0.0,0.06666666666666667});
		drawTriangleEqui(new double[]{0.26666666666666666,0.3333333333333333,0.26666666666666666},new double[]{0.6666666666666666,0.6666666666666666,0.7333333333333333});
		*/

		drawTriangle(new double[]{0.0,0.1414213562373095,0.0},new double[]{0.282842712474619,0.282842712474619,0.42426406871192857});
		drawTriangle(new double[]{0.0,0.1414213562373095,0.0},new double[]{0.42426406871192857,0.42426406871192857,0.565685424949238});
		drawTriangle(new double[]{0.9899494936611666,1.131370849898476,0.9899494936611666},new double[]{0.0,0.0,0.1414213562373095});
		drawTriangle(new double[]{0.9899494936611666,1.131370849898476,0.9899494936611666},new double[]{0.282842712474619,0.282842712474619,0.42426406871192857});
		drawTriangle(new double[]{1.2727922061357857,1.4142135623730951,1.2727922061357857},new double[]{0.0,0.0,0.1414213562373095});
		
		/*
		 drawPoint(-0.43869133765083107,-0.584171319124928);
		 drawPoint(0.7252528713498695,-0.0878321832633321);
		 drawPoint(-0.28656153369903853,0.6720035023882603);
		 drawPoint(-0.4386913376508306,0.5841713191249283);
		 drawPoint(-0.28656153369903903,-0.6720035023882601);
		 drawPoint(0.7252528713498695,0.08783218326333217);
		 drawPoint(0.10264559231402451,-0.17765804975576416);
		 drawPoint(0.10253358811827931,0.17772271540832799);
		 drawPoint(-0.20517918043230382,-6.466565256377232E-5);
		 */

		show();
		
	}	
}
	